package com.sxj.corejava.code10_异常;

/**
 * @author 石小俊
 * @date 2024年05月11日 9:59
 */
public class Test07_debug的使用 {

    public static void main(String[] args) {
        UserController userController = new UserController();
        userController.register("admin", "123456");
    }

}

class UserDao {
    public void insertUser(String username, String password) {
        System.out.println("准备注册,进行数据库的连接");
    }
}

class UserService {

    UserDao userDao = new UserDao();

    public void register(String username, String password) throws UserExistException {
        userDao.insertUser(username, password);
        int j = 3;
        for (int i = 0; i < 4; i++) {
            System.out.println(i);
            j--;
            if (j == 0) {
                throw new ArithmeticException("算术异常,除数为0");
            }
            int num = i / j;
        }
        if ("admin".equals(username)) {
            throw new UserExistException("该用户名已经被注册,请更换一个");
        }
    }
}

class UserController {
    UserService userService = new UserService();

    public void register(String username, String password) {
        try {
            userService.register(username, password);
        } catch (UserExistException e) {
            e.printStackTrace();
        }
    }
}
